//------------------------------------------------------------------------------
#include "stnmessage.h"
#include <QDebug>
//------------------------------------------------------------------------------
STNMessage::STNMessage(){
	m_message = QString();
	m_title = QString();
	m_icon = QString();
	m_program = QString();
}
//------------------------------------------------------------------------------
STNMessage::STNMessage(
	const QString &message,
	const QString &title,
	const QString &icon/*=QString()*/,
	const QString &program/*=QString()*/
){
	m_message	= message;
	m_title		= title;
	m_icon		= icon;
	m_program	= program;
}
//------------------------------------------------------------------------------
STNMessage::STNMessage(const STNMessage &src){
	clone(src);
}
//------------------------------------------------------------------------------
const STNMessage & STNMessage::operator=(const STNMessage &src){
	return clone(src);
}
//------------------------------------------------------------------------------
const STNMessage & STNMessage::clone(const STNMessage &src){
	if(this != &src){
		m_message	= src.m_message;
		m_title		= src.m_title;
		m_icon		= src.m_icon;
		m_program	= src.m_program;
	}
	return *this;
}
//------------------------------------------------------------------------------
QString STNMessage::toXml() const{
	QDomDocument doc(DOM_DOCTYPE);

	QDomElement root = doc.createElement(DOM_ROOT_ELM);
	doc.appendChild(root);

	QDomElement message = doc.createElement("m_message");
	message.appendChild(doc.createTextNode(m_message));
	root.appendChild(message);

	QDomElement title = doc.createElement("m_title");
	title.appendChild(doc.createTextNode(m_title));
	root.appendChild(title);

	QDomElement icon = doc.createElement("m_icon");
	icon.appendChild(doc.createTextNode(m_icon));
	root.appendChild(icon);

	QDomElement program = doc.createElement("m_program");
	program.appendChild(doc.createTextNode(m_program));
	root.appendChild(program);

	return doc.toString();
}
//------------------------------------------------------------------------------
bool STNMessage::loadFromXml(const QString &data){
	QString errorMsg;
	QDomDocument doc(DOM_DOCTYPE);

	if(!doc.setContent(data, &errorMsg)){
		qWarning()<<"Message::loadFromXml - Error loading message : "<<errorMsg;
		return false;
	}

	QDomElement root = doc.documentElement();
	if(root.nodeName() != DOM_ROOT_ELM){
		qWarning()<<"Message::loadFromXml() - Root name doesn't match ("<<root.nodeName()<<" != "<<DOM_ROOT_ELM<<")";
		return false;
	}

	for(QDomNode node = root.firstChild() ; !node.isNull() ; node = node.nextSibling()){
		QString name = node.nodeName();
		QString data = node.firstChild().toText().data();

		if(name == "m_message")
			m_message = data;
		else if(name == "m_title")
			m_title = data;
		else if(name == "m_icon")
			m_icon = data;
		else if(name == "m_program")
			m_program = data;
		else
			qDebug()<<"Message::loadFromXml() - Unknown node : "<<name;
	}

	return true;
}
//------------------------------------------------------------------------------
